/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.flan.stock.R;
import com.flan.stock.service.StockService;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

/**
 * App启动时的等待窗口，处理进入home页时需要预先加载的内容
 * @author flan
 * @date   2015年10月5日
 */
public class BeginActivity extends Activity{
	
	private final String PREFERENCES_NAME = "app_setting";
	
	private boolean isFirstInstall = false;
	private SharedPreferences preferences;
	private Timer timer;
	
	private Button but_skip;
	private WebView wv_begin;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_begin);
		
		but_skip = (Button) this.findViewById(R.id.but_skip);
		wv_begin = (WebView) this.findViewById(R.id.wv_begin);
		
		WebSettings webSettings = wv_begin.getSettings();  
        webSettings.setJavaScriptEnabled(false);    //设置WebView属性，能够执行Javascript脚本    
        webSettings.setAllowFileAccess(true);  		//设置可以访问文件  
        webSettings.setBuiltInZoomControls(false);  //设置支持缩放  
        wv_begin.loadUrl(getResources().getString(R.string.url_begin_ad));    
		
		
		preferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
		isFirstInstall = preferences.getBoolean("isFirstInstall", true);
		
		//这里判断是不是初次安装，初次安装进入引导页面，不是是初次安装就直接进入home页
		if(isFirstInstall){
			Intent intent = new Intent(this, WelcomeActivity.class);
			this.startActivity(intent);
			
			//更新安装状态值，设为false
			SharedPreferences.Editor editor = preferences.edit();
			editor.putBoolean("isFirstInstall", false);
			editor.commit();
			BeginActivity.this.finish();
		}else {
			timer = new Timer();
			timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					Intent intent = new Intent(BeginActivity.this, HomeActivity.class);
					BeginActivity.this.startActivity(intent);
					BeginActivity.this.finish();
				}
			}, 1*1000);
		}
		
		//启动后台服务控件
		Intent intent = new Intent(this, StockService.class);
		this.startService(intent);
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//System.out.println("执行onResume");
		
		//跳过广告按钮监听器
		but_skip.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(timer != null){
					timer.cancel();
				}
				Intent intent = new Intent(BeginActivity.this, HomeActivity.class);
				BeginActivity.this.startActivity(intent);
				BeginActivity.this.finish();
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		//System.out.println("执行onStart");
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		//System.out.println("执行onPause");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		//System.out.println("执行onStop");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//System.out.println("执行onDestroy");
	}
	
}














